import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inspiration_f/model/home_mode.dart';
import 'package:inspiration_f/page/home/home_controller.dart';
import 'package:inspiration_f/page/home/home_manager.dart';
import 'package:inspiration_f/utils/color.dart';
import 'package:inspiration_f/utils/xm_preferences.dart';

// 当前状态
enum HomeDialogStatus { showed, closed }

// 首页弹框
class HomeDialog extends StatefulWidget {
  HomeModel? homeM;
  VoidCallback? clickSaveTap;
  TextEditingController controller;
  HomeController homeController;
  HomeDialog(this.homeM, this.controller, this.homeController);
  @override
  State<HomeDialog> createState() => _HomeDialogState();
  bool isShowing = false;
  HomeDialogStatus dialogStatus = HomeDialogStatus.closed; // 首次的关闭状态
  // 弹出动画
  showAction(HomeModel? model) {
    if (isShowing) {
      return;
    }
    isShowing = true;
    dialogStatus = HomeDialogStatus.showed; // 弹开状态
    homeM = model;
    // 给输入框赋值
    controller.text = homeM?.title ?? "";
    var animationModel = HomeAnimationModel();
    animationModel.initAnimationModel();
    homeController.animationModel.value = animationModel;
    print("showAction startTop =${HomeManager.instance.startTop}");
    Future.delayed(const Duration(milliseconds: 1)).then((value) {
      var animationModel = HomeAnimationModel();
      animationModel.bigAnimationModel();
      homeController.animationModel.value = animationModel;
    });
  }

  // 隐藏动画
  hideAction(HomeModel? model) {
    if (isShowing == false) {
      return;
    }
    isShowing = false;
    homeM = model;
    var animationModel0 = HomeAnimationModel();
    animationModel0.bigAnimationModel();
    homeController.animationModel.value = animationModel0;
    Future.delayed(const Duration(milliseconds: 1)).then((value) {
      HomeAnimationModel homeAnimationModel = HomeAnimationModel();
      homeAnimationModel.smallAnimationModel();
      homeController.animationModel.value = homeAnimationModel;
    });
  }
}

class _HomeDialogState extends State<HomeDialog> {
  final FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    // 给输入框赋值
    widget.controller.text = widget.homeM?.title ?? "";
    print("initstatst");
  }

  _saveAction() {
    print("saverrrrr");
    focusNode.unfocus();
    // 内容用输入框里面最新的
    widget.homeM?.title = widget.controller.text ?? "";
    XMPreferences.instance.addHomeModel(widget.homeM);
    widget.isShowing = true;
    widget.hideAction(widget.homeM);
    Future.delayed(Duration(milliseconds: 260)).then((value) {
      widget.clickSaveTap?.call();
    });
  }

  @override
  void dispose() {
    if (kDebugMode) {
      print("disposedisposedispose");
    }
    focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return AnimatedSwitcher(
        duration: Duration(
            milliseconds:
                widget.homeController.animationModel.value.durationTime),
        child: Material(
          color: Colors.black.withOpacity(
              widget.homeController.animationModel.value.myOpacity),
          child: Stack(
            children: [
              AnimatedOpacity(
                duration: Duration(
                    milliseconds: widget
                        .homeController.animationModel.value.durationTime),
                opacity: widget.homeController.animationModel.value.myOpacity,
                child: Container(
                  color: Colors.black,
                  width: double.infinity,
                  height: double.infinity,
                ),
              ),
              InkWell(
                highlightColor: Colors.transparent,
                radius: 0,
                onTap: _saveAction, // 关闭页面，保存数据
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  alignment: Alignment.topLeft,
                ),
              ),
              _inputView(), // 输入框
              _colorsView(), // 颜色选择view
            ],
          ),
        ),
      );
    });
  }

  // 输入框
  Widget _inputView() {
    return Obx(() {
      return AnimatedPositioned(
        curve: widget.homeController.animationModel.value.curve,
        duration: Duration(
            milliseconds:
                widget.homeController.animationModel.value.durationTime),
        top: widget.homeController.animationModel.value.top,
        left: 16,
        right: 16,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: AnimatedContainer(
            curve: widget.homeController.animationModel.value.curve,
            duration: Duration(
                milliseconds:
                    widget.homeController.animationModel.value.durationTime),
            height: widget.homeController.animationModel.value.contentHeight,
            color: XMColor.getColor(widget.homeM?.colorInt ?? 0).withOpacity(
                widget.homeController.animationModel.value.contentOpacity),
            padding:
                const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 12),
            child: TextField(
              autofocus: true,
              maxLines: null,
              focusNode: focusNode,
              controller: widget.controller,
              textInputAction: TextInputAction.done,
              style: TextStyle(
                  fontSize: 16,
                  color: Colors.white.withOpacity(widget
                      .homeController.animationModel.value.contentOpacity)),
              cursorColor: Colors.white,
              decoration: const InputDecoration(
                  filled: false, border: InputBorder.none),
              onEditingComplete: () {
                _saveAction();
              },
            ),
          ),
        ),
      );
    });
  }

  // 颜色选择view
  Widget _colorsView() {
    double colorWidth = MediaQuery.of(context).size.width;
    double space = 20;
    return Obx(() {
      return AnimatedPositioned(
        curve: widget.homeController.animationModel.value.curve,
        top: widget.homeController.animationModel.value.top -
            widget.homeController.animationModel.value.colorWidth / 2.0,
        left: 0,
        duration: Duration(
            milliseconds:
                widget.homeController.animationModel.value.durationTime),
        child: SizedBox(
          width: colorWidth,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                width: space,
                height: 1,
              ),
              _oneColorView(xmGreen, 0),
              SizedBox(
                width: space,
                height: 1,
              ),
              _oneColorView(xmBlue, 1),
              SizedBox(
                width: space,
                height: 1,
              ),
              _oneColorView(xmYellow, 2),
              SizedBox(
                width: space,
                height: 1,
              ),
              _oneColorView(xmRed, 3),
              SizedBox(
                width: space,
                height: 1,
              ),
              _oneColorView(xmPurple, 4),
              SizedBox(
                width: space,
                height: 1,
              ),
            ],
          ),
        ),
      );
    });
  }

  // 一个颜色view
  Widget _oneColorView(Color color, int colorInt) {
    return InkWell(
      highlightColor: Colors.transparent,
      radius: 0,
      onTap: () {
        setState(() {
          widget.homeM?.colorInt = colorInt;
        });
      },
      child: AnimatedContainer(
        curve: widget.homeController.animationModel.value.curve,
        duration: Duration(
            milliseconds:
                widget.homeController.animationModel.value.durationTime),
        decoration: BoxDecoration(
          color: color.withOpacity(
              widget.homeController.animationModel.value.contentOpacity),
          borderRadius: BorderRadius.circular(
              widget.homeController.animationModel.value.colorWidth / 2),
          border: Border.all(color: Colors.white, width: 2),
        ),
        width: widget.homeController.animationModel.value.colorWidth,
        height: widget.homeController.animationModel.value.colorWidth,
      ),
    );
  }
}
